home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cocktail
/
l2r.lha
/
l2r
/
lex.lalr
< prev
next >
Wrap
Text File
|
1992-08-20
|
5KB
|
197 lines
/************************************************************************/
/* */
/* specification of parser for lex input language in lalr syntax */
/* */
/************************************************************************/
GLOBAL {
#include <stdio.h>
typedef struct { tScanAttribute Scan; } tParsAttribute;
static int in_defs = 1; /* parsing definition part */
static char macro [256];/* regular expression for macro definition */
static int m; /* index for macro */
static int level = 0; /* level count to decide on meaning of $ */
/* output representation for character, see print_char */
static char how_to_print [] =
{ 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 0 };
/* in definition part, append v to macro, otherwise copy to buffer */
static void str_put (v)
char * v;
{
if (in_defs) {
while (macro [m++] = *v++);
m--;
}
else BufString (v);
}
/* print one character, escape if necessary */
static void print_char (c)
char c;
{
char i;
char v [8];
i = how_to_print [c];
switch (i) {
case 0 : (void) sprintf (v, "\\%d", c); break;
case 1 : (void) sprintf (v, "%c", c); break;
case 2 : (void) sprintf (v, "\\%c", c); break;
default : (void) sprintf (v, "\\%c", i); break;
}
str_put (v);
}
/* print the default rules for lex compatibility */
default_rules ()
{
str_put ("\" \" :- { yyEcho; }\n");
str_put ("\\t :- { yyTab; yyEcho; }\n");
str_put ("\\n :- { yyEol (0); yyEcho; }\n");
}
}
TOKEN
'%%' = 1
STARTDEF = 4
'%t' = 5
'/' = 6
'.' = 7
'?' = 8
'+' = 9
'*' = 10
'(' = 11
')' = 12
'|' = 13
ACTION = 14
STARTCOND = 15
REPEAT = 16
EXPANSION = 17
'^' = 18
PSEUDOACTION = 19
STRING = 20
CHARCLASS = 21
CHAR = 22
MACRODEF = 23
TRANSLATION = 24
'\n' = 25
'$' = 26
RULE
(************************************************************************)
(* *)
(* syntax of lex source file *)
(* *)
(* lex source consists of a definition part, a rule part and an *)
(* optional subprograms part, which are separated by lines beginning *)
(* with '%%'. *)
(* *)
(************************************************************************)
source : defs '%%' { in_defs = 0; str_put ("\nRULES\n\n"); }
rules { default_rules (); } [ '%%' ] .
(************************************************************************)
(* *)
(* definition part *)
(* *)
(* consists of an arbitrary number of definitions (even 0). *)
(* *)
(************************************************************************)
defs : ( def '\n' ) * .
(* syntax of single definition *)
def : macrodef expr { str_put (" ."); put_macro (macro); level--; }
| STARTDEF
| '%t' ( TRANSLATION '\n' ) * '%t'
{ (void) fprintf (stderr, "WARNING: translation table ignored !\n"); }
|
.
macrodef : MACRODEF { m = 0;
str_put ((char *) $1.Scan.string_value);
str_put (" = ");
level++; } .
(************************************************************************)
(* *)
(* rule part *)
(* *)
(* consists of an arbitrary number of rules (even 0). *)
(* *)
(************************************************************************)
rules : ( rule '\n' { str_put ("\n"); } ) * .
rule : [ startcond caret fullexpr [ endcond ] ] action .
startcond : STARTCOND { str_put ((char *) $1.Scan.string_value); str_put (" "); } .
startcond : .
caret : '^' { str_put ("< "); } .
caret : .
endcond : '/' { str_put (" / "); } expr .
fullexpr : fullexpr '|' { str_put (" , "); } term .
fullexpr : term .
expr : expr '|' { str_put ("|"); } term .
expr : term .
term : factor repeat .
repeat : REPEAT { str_put ((char *) $1.Scan.string_value); } .
repeat : .
factor : factor secondary .
factor : secondary .
secondary : primary '?' { str_put ("?"); }
| primary '*' { str_put ("*"); }
| primary '+' { str_put ("+"); }
| primary
.
primary : '(' { str_put ("("); level++; } expr ')'
{ str_put (")"); level--; }
| EXPANSION { str_put ((char *) $1.Scan.string_value); }
| CHARCLASS { str_put ((char *) $1.Scan.string_value); }
| '.' { str_put (" ANY "); }
| STRING { str_put ((char *) $1.Scan.string_value); }
| CHAR { print_char ((char) $1.Scan.char_value); }
| '$' { if (level != 0)
{ str_put ("$"); }
else
str_put (" >"); }
.
action : { str_put (" : { (void) GetWord (yytext); "); }
action_1 { str_put (" }"); }
| PSEUDOACTION { str_put (" ,"); }
|
.
action_1 : action_1 action_2 .
action_1 : action_2 .
action_2 : ACTION { str_put ((char *) $1.Scan.string_value); } .